local plugin = {
    PRIORITY = 1000,
    VERSION = "0.1"
}

function plugin:init_worker()
    kong.log.debug("init_worker() called")
end

function plugin:configure(configs)
    kong.log.notice("saying hi from the 'configure' handler, got ", (configs and #configs or 0), " configs")

    if configs == nil then
        return -- no configs, nothing to do
    end

    -- your custom code here

end

function plugin:rewrite(conf)
    -- your custom code here
    kong.log.debug("saying hi from the 'rewrite' handler")
end

function plugin:access(conf)
    plugin.super.access(self) -- call the parent implementation

    kong.log.debug("access() called")

    local pattern = plugin_conf.pattern
    local token = kong.request.get_header("authorization")
    if token == nil then
        return
    end
    -- 忽略大小写
    local matched = ngx.re.match(token, pattern, "joi")
    if matched then
        -- 设置upstream
        local ok, err = kong.service.set_upstream(conf.upstream)
        if not ok then
            kong.log.err(err)
            return
        end
        -- 匹配成功添加特定头部方便监控
        ngx.req.set_header("X-Kong-" .. plugin_name .. "-upstream", conf.upstream)
        ngx.req.set_header("X-Kong-" .. plugin_name .. "-pattern", conf.pattern)
    end

end

function plugin:body_filter(conf)
    -- your custom code here
    kong.log.debug("saying hi from the 'body_filter' handler")

end

function plugin:log(conf)
    -- your custom code here
    kong.log.debug("saying hi from the 'log' handler")

end

return plugin
